
var getid='';
var  spec_i = '' ;							
var val_s = '';
var htm_s = '' ;
var coin_type = '' ;
//规格数组
var size_json = new Array();

var name_h = '' ;
var size1 = '' ;
var size2 = '' ;
// 按规格存储规格值数据
var spec_group_checked = ['',''];
var str = '';
var V = new Array();
var spec_group_checked_0 = new Array();
var spec_group_checked_1 = new Array();
var host = window.location.host;

//自动计算
$('input[name="g_marketprice"]').bind('change',function(){
	gc_price = parseFloat($('input[name="g_price"]').val());
	gc_marketprice = parseFloat($('input[name="g_marketprice"]').val());
	if (gc_price && gc_marketprice) {								
		gc_discount = Math.round(gc_price/gc_marketprice *100 );
		//自动计算积分
		var points = $('input[name="point_num"]').val();
		if (!points) {
			var gc_points = Math.round(gc_marketprice - gc_price);
			$('input[name="point_num"]').val(gc_points);
		}
		$('input[name="g_discount"]').val(gc_discount);
	}
});	

$('input[name="g_price"]').bind('change',function(){
	gc_price = parseFloat($('input[name="g_price"]').val());
	gc_marketprice = parseFloat($('input[name="g_marketprice"]').val());
	
	if (gc_price && gc_marketprice) {
		gc_discount = Math.round(gc_price/gc_marketprice *100 );
		//自动计算积分
		var points = $('input[name="point_num"]').val();
		if (!points) {
			var gc_points = Math.round(gc_marketprice - gc_price);

			$('input[name="point_num"]').val(gc_points);
		}
										
		$('input[name="g_discount"]').val(gc_discount);
									
	}
});	

//添加规格
$("[nctype='specAddSubmit']").live('click',function(){
	var vals = $(this).siblings("input").val();
	var parent = $(this).parent().parent().parent().parent().parent().find("input").attr("name");
								
	var num = $(this).parent().parent().parent().parent().find('li').length;
	var leg = num - 2 ;
	var nc_type = $(this).parent().parent().parent().parent().find('li').eq(leg).find("input[type='checkbox']").attr("nc_type");
								
	if (!nc_type) {
		nc_type = 1 ;
	} else {
		nc_type = parseInt(nc_type)+1;
	} 
							
	if (vals) {	
		if (parent=='sp_name[1]') {
			var size_n = 'size1' ;
		} else if (parent=='sp_name[2]') {
			var size_n = 'size2' ;
		}
		var html = '<li><span nctype="input_checkbox"><input type="checkbox" value="'+vals+'" class="sp_val" nc_type="'+nc_type+'" name="'+parent+'['+nc_type+'][name]"></span><span nctype="pv_name">'+vals+'</span>';
									
		html += '<input type="hidden" value="'+nc_type+'" class="sp_val" name="'+parent+'['+nc_type+'][id]" ><input type="hidden" value="" class="sp_val" name="'+parent+'['+nc_type+'][size]" nctype="'+size_n+'">';

		html += '</li>';
									
		var xml = $(this).parent().parent().prev('li').html();			
									
		if (xml) {
			$(this).parent().parent().prev('li').after(html);
		} else {
			$(this).parent().parent().before(html);
		}									
	}
								
});


//规格选择
$('dl[nctype="spec_group_dl"]').on('click', 'span[nctype="input_checkbox"] > input[type="checkbox"]',function(){
	if($(this).is(':checked')){
						
		var nc_type = $(this).attr("nc_type");
		var parent = $(this).parent().parent().parent().parent().parent().find("input").attr("name");
		var size_n = '' ;
		if (parent=='sp_name[1]') {
			size_n = "size1" ;
			size_v = size2 ;
		} else if(parent=='sp_name[2]'){
			size_n = "size2" ;
			size_v = size1 ;
		}
							
		if ($(this).val()) {
			var html = '<input type="hidden" value="'+nc_type+'" class="sp_val" name="'+parent+'['+nc_type+'][id]" ><input type="hidden" value="" class="sp_val" name="'+parent+'['+nc_type+'][size]" nctype="'+size_n+'">';
			$(this).parent().parent().find('span[nctype="pv_name"]').after(html) ;							
		}
	} else {
						
		$(this).parent().parent().find('input[type="hidden"]').remove();
	}		
	into_array();
	goods_stock_set(val_s);
	$('input[nctype="size1"]').val(size2);
	$('input[nctype="size2"]').val(size1);
	//销毁变量
	size1 = '' ;
	size2 = '' ;
});						
						
$(function(){
	// 提交后不没有填写的价格或库存的库存配置设为默认价格和0
	// 库存配置隐藏式 里面的input加上disable属性
	$('input[type="submit"]').click(function(){
		$('input[data_type="price"]').each(function(){
			if($(this).val() == ''){
				$(this).val($('input[name="g_price"]').val());
			}
		});
		$('input[data_type="point"]').each(function(){
			if($(this).val() == ''){
				$(this).val($('input[name="point_num"]').val());
			}
		});
		$('input[data_type="coin"]').each(function(){
			if($(this).val() == ''){
				$(this).val($('input[name="coin_num"]').val());
			}
		});
		$('input[data_type="stock"]').each(function(){
			if($(this).val() == ''){
				$(this).val('0');
			}
		});
		$('input[data_type="alarm"]').each(function(){
			if($(this).val() == ''){
				$(this).val('0');
			}
		});
		if($('dl[nc_type="spec_dl"]').css('display') == 'none'){
			$('dl[nc_type="spec_dl"]').find('input').attr('disabled','disabled');
		}
	});
						
});				

//修改规格名字			
$('dl[nctype="spec_group_dl"]').on('change', 'span[nctype="pv_name"] > input[type="text"]', function() {
		var vals = $(this).val() ;
		var prent = $(this).parent().parent().find('span[nctype="input_checkbox"] input').val(vals);
		//alert(prent);
							
});

// 将选中的规格放入数组
function into_array(){
					
		spec_group_checked_0 = new Array();
		$('dl[nc_type="spec_group_dl_0"]').find('input[type="checkbox"]:checked').each(function(){
						
			v = $(this).val();
						
			if (v) {
				i = $(this).attr('nc_type');
				c = null;
							
				if ($(this).parents('dl:first').attr('spec_img') == 't') {
					c = 1;
				}
				size1 += i+',';
			}else {
				i = '';
				c = null;
			}
												
			spec_group_checked_0[spec_group_checked_0.length] = [v,i,c];
		});

		spec_group_checked[0] = spec_group_checked_0;
				
		spec_group_checked_1 = new Array();
		$('dl[nc_type="spec_group_dl_1"]').find('input[type="checkbox"]:checked').each(function(){
						
			v = $(this).val();
			if (v) {
				i = $(this).attr('nc_type');
				c = null;
				if ($(this).parents('dl:first').attr('spec_img') == 't') {
					c = 1;
				}
				size2 += i+',' ;
			} else {
				i = '';
				c = null;
			}
						
			spec_group_checked_1[spec_group_checked_1.length] = [v,i,c];

		});

		spec_group_checked[1] = spec_group_checked_1;
}
						
function goods_stock_set(val_s){
			
		price_name(name_h,val_s);
		//  店铺价格 商品库存改为只读
		$('input[name="g_price"]').attr('readonly','readonly').css('background','#E7E7E7 none');
		$('input[name="point_num"]').attr('readonly','readonly').css('background','#E7E7E7 none');
		$('input[name="coin_num"]').attr('readonly','readonly').css('background','#E7E7E7 none');
		$('input[name="g_marketprice"]').attr('readonly','readonly').css('background','#E7E7E7 none');
		$('input[name="g_storage"]').attr('readonly','readonly').css('background','#E7E7E7 none');	
				
		$('dl[nc_type="spec_dl"]').show();
		str = '<tr>';
		//td_1颜色 td_2规格
				
		for (var i_0=0; i_0<spec_group_checked[0].length; i_0++){
			td_1 = spec_group_checked[0][i_0];
					
			for (var i_1=0; i_1<spec_group_checked[1].length; i_1++){
					
				td_2 = spec_group_checked[1][i_1];
				var tmp_spec_td = new Array();
						
				//序号
				tmp_spec_td[0] = td_1[1];//颜色序号
				tmp_spec_td[1] = td_2[1];//规格序号

				if (td_1[1] && td_2[1]) {
					//tmp_spec_td.sort(function(a,b){return a-b});
					var spec_bunch = '';
					var mar_v = '' ;
					var pr_v = '' ;
					var po_v = '' ;
					var st_v = '' ;
					var al_v = '' ;
					var sku_v = '' ;						
					var vlue = '' ;
							
					spec_bunch += tmp_spec_td[0]+"|";
					spec_bunch += tmp_spec_td[1];
					
					if (gid) {
						var url = "/store/store_goods_add?op=add_step_two&goodsid="+gid ;
						
						vlue =  size_info(gid,url,spec_bunch);
						
						if (vlue) {
							 mar_v = vlue.marketprice ;
							 pr_v = vlue.price ;
							 po_v =  vlue.point_num ;
							 st_v =  vlue.stock ;
							 al_v = vlue.alarm ;
							 sku_v = vlue.sku ;	
							 if (!mar_v) {
								mar_v = '' ;
							}
							if (!pr_v) {
								pr_v = '' ;
							}
							if (!st_v) {
								st_v = '' ;
							}
							if (!al_v) {
								al_v = '' ;
							}
							if (!sku_v) {
								sku_v = '' ;
							}	
							if (!po_v) {
								po_v = '' ;
							}							
							 $('input[name="coin_num"]').val(pr_v);
								$('input[name="point_num"]').val(po_v);
							 $('input[name="g_price"]').val(pr_v);
						}

					}
							
					str +='<td><input type="hidden" value="'+td_1[0]+'" />'+td_1[0]+'</td>';

					str +='<td><input type="hidden" value="'+td_2[0]+'" />'+td_2[0]+'</td>';
													
					str +='<td><input class="text price" type="text" name="spec['+spec_bunch+'][marketprice]" data_type="marketprice" nc_type="'+spec_bunch+'|marketprice" value="'+mar_v+'" /><em class="add-on"><i class="icon-renminbi"></i></em></td>';

					if(3==val_s){
						str +='<td><input class="text price" type="text" name="spec['+spec_bunch+'][price]" data_type="price" nc_type="'+spec_bunch+'|price" value="'+pr_v+'" /><em class="add-on"><i class="icon-renminbi"></i></em></td>';
						str +='<td><input class="text price" type="text" name="spec['+spec_bunch+'][point_num]" data_type="point_num" nc_type="'+spec_bunch+'|point_num" value="'+po_v+'" /><em class="add-on"><i class="icon-renminbi"></i></em></td>';
								
					} else {
						str +='<td><input class="text price" type="text" name="spec['+spec_bunch+'][price]" data_type="price" nc_type="'+spec_bunch+'|price" value="'+pr_v+'" /><em class="add-on"><i class="icon-renminbi"></i></em></td>';
							
					}											
							
					str +='<td><input class="text stock" type="text" name="spec['+spec_bunch+'][stock]" data_type="stock" nc_type="'+spec_bunch+'|stock" value="'+st_v+'" /></td><td><input class="text stock" type="text" name="spec['+spec_bunch+'][alarm]" data_type="alarm" nc_type="'+spec_bunch+'|alarm" value="'+al_v+'" /></td><td><input class="text sku" type="text" name="spec['+spec_bunch+'][sku]" nc_type="'+spec_bunch+'|sku" value="'+sku_v+'" /></td></tr>';						
				}
		
			}
		}

		if(str == '<tr>'){
				
			//  店铺价格 商品库存取消只读
			$('input[name="g_price"]').removeAttr('readonly').css('background','');
			$('input[name="g_storage"]').removeAttr('readonly').css('background','');
			$('input[name="point_num"]').removeAttr('readonly').css('background','');
			$('input[name="coin_num"]').removeAttr('readonly').css('background','');
			$('input[name="g_marketprice"]').removeAttr('readonly').css('background','');
			$('dl[nc_type="spec_dl"]').hide();
		}else{
				
			$('tbody[nc_type="spec_table"]').empty().html(str).find('input[nc_type]').each(function(){
					
				var ssval = '' ;
				s = $(this).attr('nc_type');
					
				var ssval = $('input[nc_type="'+s+'"]').val();
				//alert(ssval);
				if (ssval<0) {
					try{$(this).val(V[s]);}catch(ex){$(this).val('');};
				}
					
				if ($(this).attr('data_type') == 'marketprice' && $(this).val() == '') {
					
					$(this).val($('input[name="g_marketprice"]').val());
				}
					
				if (1==val_s) {
					if ($(this).attr('data_type') == 'price' && $(this).val() == ''){
						$(this).val($('input[name="g_price"]').val());
					}

				} else if (2==val_s) {
					if ($(this).attr('data_type') == 'price' && $(this).val() == ''){
						$(this).val($('input[name="point_num"]').val());
					}		
				} else if (3==val_s) {
					if ($(this).attr('data_type') == 'price' && $(this).val() == ''){
						$(this).val($('input[name="price"]').val());
					}
					if ($(this).attr('data_type') == 'point_num' && $(this).val() == ''){
						$(this).val($('input[name="point_num"]').val());
					}
				} else if (4==val_s) {
					if ($(this).attr('data_type') == 'price' && $(this).val() == ''){
						$(this).val($('input[name="g_price"]').val());
					}
				} else if (5==val_s) {
						
					if ($(this).attr('data_type') == 'price' && $(this).val() == ''){
							
						$(this).val($('input[name="coin_num"]').val());
					}
				}

				if ($(this).attr('data_type') == 'stock' && $(this).val() == ''){
					$(this).val('0');
				}
				if ($(this).attr('data_type') == 'alarm' && $(this).val() == ''){
					$(this).val('0');
				}
					
			}).end().find('input[data_type="stock"]').change(function(){
				computeStock();    // 库存计算
			}).end().find('input[data_type="price"]').change(function(){
				computePrice();     // 价格计算
			}).end().find('input[nc_type]').change(function(){
				s = $(this).attr('nc_type');
				V[s] = $(this).val();
			});
			}
}
		
/* 获取商品规格 */
function size_info(gid,url,spec_i) {
	//getid = '' ;
	if (getid!=gid) {
		getid = gid ;
		size_json = false ;		
	}
	if (!size_json) {
		$.ajax({
			type:'post',
			url:url,
			data:{op:'add_step_two',sizeinfo:1,gid:gid},
			dataType:'json',
			async:false,
			success:function(data){
				size_json = data ;
			}
			
		});		
	}
	
	if (size_json[spec_i]){
		
		return size_json[spec_i] ;
	} else {
		return size_json ;
		
	}	
}

function price_name(name_h,val_s){
	// 1 现金区 ,2 积分区 积分价, 3 积分+现金区, 4 抢购区 , 5 增值区 增值币价
	
	if (1==val_s) {
		name_h = '价格' ;
		$("#points_th").parent().hide();	
		$("#market_th").parent().show();

	} else if (2==val_s) {
		name_h = '积分价' ;
		$("#points_th").parent().hide();
		$("#market_th").parent().show();
		coin_type = 'point_num' ;					
	} else if (3==val_s) {
		name_h = '商品价' ;
		$("#market_th").parent().show();
		$("#points_th").parent().show();

	} else if (4==val_s) {
		name_h = '价格' ;
		$("#points_th").parent().hide();
		$("#market_th").parent().hide();
	} else if (5==val_s) {
		name_h = '增值币价' ;
		$("#points_th").parent().hide();
		$("#market_th").parent().show();
		//改变属性值				
		coin_type = 'coin_num' ;
	}
				
	$("#market_th").html(name_h);	
	
}
//获取最小值
function min_value(size_json){
	
	if(!!size_json){
		
		var url = "/store/store_goods_add?op=add_step_two&goodsid="+gid ;
		$.ajax({
			type:'post',
			url:url,
			data:{op:'add_step_two',sizeinfo:1,gid:gid},
			dataType:'json',
			async:false,
			success:function(data){
				size_json = data ;				
			}	
			
		});	
		
	}
	var min = '' ;
	var p_min = '' ;
	
	$.each(size_json, function(key,val) {
		if(val.price){
			if(!min){
				min = val.price;
			}
			if(val.price < min){
				min = val.price ;
			}
			if (val.point_num) {
				if(!p_min){
					p_min = val.point_num;
				}
				if(val.point_num < p_min){
					p_min = val.point_num ;
				}				
			}
		}
		
	} );
	var result = '' ;
	if(min){
		if(!p_min){
			p_min = 0 ;
		}
		result ={min:''+min+'',p_min:''+p_min+''};
	}else{
		result ={min:0,p_min:0};
	}
	
	return result ;
	
}

function uploadImage(id,srcid) {
	var data = new FormData();
	$.each($('#'+id)[0].files, function(i, file) {
		data.append('upload_file', file);
	});
	data.append('store', 1);

	$.ajax({
			type: "POST",
			url: "/store/pic_upload",
			data: data,
			contentType:'multipart/form-data',
			cache: false,
			contentType: false,    //不可缺
			processData: false,    //不可缺
			success: function(data) {
				if (srcid=='add_album_i') {
					var img_html = '<img src="'+data.info+'" data-ke-src="'+data.info+'">' ;
					KE.insertHtml(img_html);
				} else {
					if (id=="goods_image") {
						$("#image_path").val(data.info);
					}
					$("#"+srcid).empty();
											
					$("#"+srcid).attr('src',data.info);
					$("#"+srcid).show();									
				}

			},
			error: function(XMLHttpRequest, textStatus, errorThrown){
				alert("上传失败，请检查网络后重试");
			}
	});
}

//上传图片
$("#goods_image").change(function(){
	
	uploadImage('goods_image','demo_image');
});
						
$("#add_album").change(function(){
	//上传图片
	uploadImage('add_album','add_album_i');
});
						
$('#show_image').live('click',function() {
	var state = 'show_image';
	$('#show_image').hide();
	$('#del_goods_demo').show();
	imgset(state);								
	$("#demo").show();
}); 
$('#show_desc').live('click',function() {
	var state = 'show_desc';
	$('#show_desc').hide();
	$('#del_desc').show();
	imgset(state);
	$("#des_demo").show();
});	
$('#del_goods_demo').live('click',function() {

	$('#show_image').show();
	$('#del_goods_demo').hide();
	$("#demo").hide();
}); 
$('#del_desc').live('click',function() {
	$('#show_desc').show();
	$('#del_desc').hide();
	$("#des_demo").hide();
});	
//获取图片 上传一次刷新一次
function imgset(state) {
	$.ajax({
		type: "POST",
		url: "/store/picset",
		data:{state:state},
		dataType:'json',
		success: function(data) {
			var e = data.length;
			var id ='';
			if (state=='show_image'){
				id = 'demo_image';
			} else if(state=='show_desc') {
				id = 'g_body';
			}

			var html = '<div class="goods-gallery "><a class="sample_demo"  href="" style="display:none;">提交</a><div class="nav"><span class="l">用户相册 &gt;全部图片 </span><span class="r"><select name="jumpMenu" style="width:100px;"><option value="0" style="width:80px;">请选择...</option><option style="width:80px;" value="2">默认相册</option></select></span></div><ul class="list" id="list_li">';
										
			for(var i=0;i<e;i++){
				html += '<li><a href="JavaScript:void(0);" data_pid ="'+id+'" ><img src="'+data[i]+'" title="bobo_logo"></a></li>';
											
			}
			html += '<div class="pagination"><ul><li><span>首页</span></li><li><span>上一页</span></li><li><span class="currentpage">1</span></li><li><span>下一页</span></li><li><span>末页</span></li></ul></div></div>';
			if (state=='show_image') {
				$("#demo").html(html);
			} else if(state=='show_desc') {
				$("#des_demo").html(html);
			}
		}
	});								
}
//选择图片
$("#list_li li a").live('click',function(){
	var pid = $(this).attr('data_pid');
	var img = $(this).find('img').attr('src');
													
	if (pid=='demo_image') {	
		$("#image_path").val(img);
		$('#demo_image').attr('src',img);
	} else if(pid=='g_body'){
		var img_html = '<img src="'+img+'" data-ke-src="'+img+'">' ;
		var son = $('#g_body').val();
										
		KE.insertHtml(img_html);
	}
}); 

$("#select_pca select").live('change',function(){
								
	var data_id = $(this).val();
	var option = $(this).find("option[value='"+data_id+"']").html();
									
	var next = $(this).next("select").html();         
	var id = $(this).attr('id');
	if(id=='shop_province'){
		$("#_area_1").val(data_id);
	}else if(id=='shop_city'){
		$("#_area_2").val(data_id);
	}

	var name ='city';
	var length = $("#select_pca select").length;		
	var next_p = '';
	var province = $('#shop_province').val();
	var city = $('#shop_city').val();
	var area = $('#shop_area').val();
	var address = '';	
	if (province) {
		address = province;
	}
	if (city==option) {
		address = address +' '+ city;
	} 
									
	if (area==option) {
		address = address +' '+ city +' '+ area;
	}
									
	$('#shop_address').val(address);		
									
	if (option) {
		if (next) {
											
			if (length<3) {
				name ='shop_city';
			} else {
				if (id=='shop_province') {						
					name ='shop_city';
					next_p = 1;
				} else {
					name ='shop_area';
				}					
			}
		} else {
			if (length<2) {
				name ='shop_city';					
			} else {
				name ='shop_area';
			}			
		}

		if (id!=='shop_area' && id!=='shop_city') {
			get_area(data_id,next,$(this),name,next_p);	
		}						
	} else {			
		$(this).next("select").remove();
	}

});

//市级、区数据
function get_area(id,next,option,name,next_p) {
	var url= '/Shop/code_chage';		
	$.ajax({
		url:url,
		data:{id:id},
		type:'post',
		dataType:'json',
		success:function (data) {
			var a = data;
			var html = '';
			for(var i=0;i<a.length;i++ ) {								
				html += '<option value ="'+a[i].area_id+'" data-id="'+a[i].area_id+'">'+a[i].area_name+'</option>';
			}
											
			if (html) {
				if (next) {	
					if (next_p) {
						option.nextAll("select").html('<option value="">-请选择-</option>'+html);
						option.next().nextAll("select").remove();
					} else {
						option.next("select").html('<option value="">-请选择-</option>'+html);
					}
													
				} else {
					option.after('<select id="'+name+'"><option value="">-请选择-</option>'+html+'</select>');
				}				
			}
		}
	});		
}
